package cn.gson.boot.model.dao;

import cn.gson.boot.model.pojos.Contacts;
import cn.gson.boot.model.pojos.Hetong;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface ContactsDao extends CrudRepository<Contacts,Integer> {
    //客户合并（逻辑删除）
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery = true,value = "update contacts set k_id = ?1 where k_id=?2")
    public void kehuhebing(Integer b,Integer a);

    //客户合并（联系人表客户查询）
    @Query(nativeQuery = true,value = "select * from contacts where  k_id = ?1")
    public List<Contacts> contactscha(Integer a);

    //删除客户的时候同时删除联系人（逻辑删除）
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery = true,value = "update  contacts set c_ztai=0 where  k_id = ?1")
    public void delContacts(Integer id);

    //删除客户的时候同时删除联系人（逻辑删除）
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery = true,value = "update  contacts set c_ztai=1 where  k_id = ?1")
    public void hyContacts(Integer id);

    //根据客户联系人移动手机电话查询
    @Query(nativeQuery = true, value = "select  count(*) from contacts where c_mphone= ?1")
    public int findKehuByMphone(String mphone);

    //根据客户联系人移动手机和工作电话查询
    @Query(nativeQuery = true, value = "select  count(*) from contacts where c_mphone= ?1 or c_wphone=?2")
    public int findKehuByMphone2(String mphone,String wphone);

    //根据客户的员工id  查出客户id再查出联系人
    @Query(nativeQuery = true, value = "select  c.k_id from contacts c inner join kehu k on c.k_id = k.k_id where k.emp_id=?1")
    public List<Integer>  chacontact(int empid);
}
